<template>
  <view>
    <uni-list>
      <uni-list-item :show-arrow="false">
        你需要支付${{course.price}}来购买[ {{course.name}} ]这个课程.
      </uni-list-item>
    </uni-list>
    <button type="primary" @click="handldSub">提交订单</button>
    <button type="default" @click="handSuccess">测试支付成功</button>
    <button type="warn" @click="handError">测试支付失败</button>
  </view>
</template>

<script>
import { uniList } from "@dcloudio/uni-ui";
import { uniListItem } from "@dcloudio/uni-ui";
import { uniTag } from "@dcloudio/uni-ui";
export default {
  components: { uniList, uniListItem, uniTag },
  data() {
    return {
      cId: 0,
      course: {},
      payId: 0
    };
  },
  methods: {
    getCourse() {
      this.request({
        url:
          this.apiServer +
          "/courses/findCourseByIdAndUId?id=" +
          this.cId +
          "&uId=" +
          uni.getStorageSync("user").id
      }).then(res => {
        console.log("findCourseByIdAndUId");
        console.log(res);
        this.course = res.data;
      });
    },
    handSuccess() {
      this.request({
        url: this.apiServer + "/pay/paidPayById",
        method: "PUT",
        data: {
          id: this.payId
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        }
      }).then(res => {
        console.log(res);
      });

      uni.showToast({
        title: "支付成功!",
        icon: "success"
      });
      uni.navigateTo({
        url: `/pages/courseDetail/index?id=${this.course.id}
        &imgUrl=${this.course.imgUrl}&type=${this.course.type}`
      });
    },
    handError() {
      uni.showToast({
        title: "支付失败!请重新支付",
        icon: "none"
      });
    },
    handldSub() {
      this.request({
        url: this.apiServer + "/pay/addPay",
        method: "POST",
        data: {
          uId: uni.getStorageSync("user").id,
          orderId: this.cId,
          payTrade: "测试订单",
          totalPrice: this.course.price
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        }
      }).then(res => {
        console.log("addpay");
        this.payId = res.data;
        console.log(res);
        console.log(this.payId);
      });
    }
  },

  onLoad(option) {
    this.cId = option.cId;
    this.getCourse();
  }
};
</script>

<style>
</style>